package cn.com.pajx.pajx_visitor_v3.ui.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.provider.Settings;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.LinkedHashMap;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.pajx.pajx_visitor_v3.R;
import cn.com.pajx.pajx_visitor_v3.api.Api;
import cn.com.pajx.pajx_visitor_v3.base.BaseMvpActivity;
import cn.com.pajx.pajx_visitor_v3.bean.AppBean;
import cn.com.pajx.pajx_visitor_v3.bean.EventMessage;
import cn.com.pajx.pajx_visitor_v3.mvp.presenter.LoadDataPresenterImpl;
import cn.com.pajx.pajx_visitor_v3.ui.fragment.BindSchoolFragment;
import cn.com.pajx.pajx_visitor_v3.ui.fragment.ModifyPasswordFragment;
import cn.com.pajx.pajx_visitor_v3.ui.fragment.UnbindSchoolFragment;
import cn.com.pajx.pajx_visitor_v3.ui.fragment.UpdateAppFragment;
import cn.com.pajx.pajx_visitor_v3.utils.AppConstant;
import cn.com.pajx.pajx_visitor_v3.utils.CommonUtil;
import cn.com.pajx.pajx_visitor_v3.utils.LogUtils;
import cn.com.pajx.pajx_visitor_v3.utils.ToastUtil;
import cn.com.pajx.pajx_visitor_v3.utils.UpdateUtil;

/**
 * 设置页面
 */
@SuppressLint("NonConstantResourceId")
public class SettingActivity extends BaseMvpActivity<LoadDataPresenterImpl> {

    @BindView(R.id.tv_school_name)
    TextView tvSchoolName;
    @BindView(R.id.tv_app_version)
    TextView tvAppVersion;

    @Override
    protected void beforeSetContentView() {
        super.beforeSetContentView();
    }

    @Override
    protected LoadDataPresenterImpl initPresenter() {
        return new LoadDataPresenterImpl();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initView() {
        setHeadTitle("设置");
        EventBus.getDefault().register(this);
        loadSchoolName();
        initDownTimer();
        //设置布局显示
        showView();
    }

    /**
     * 设置布局显示
     */
    @SuppressLint("SetTextI18n")
    private void showView() {
        tvSchoolName.setText(loadSchoolName());
        tvAppVersion.setText("V" + CommonUtil.getVersionName(mContext));
    }

    /**
     * 获取APP最新版本
     */
    private void checkAppVersion() {
        mPresenter.loadData(Api.APP_UPDATE, new LinkedHashMap<>(), "APP_UPDATE", "努力加载中");
    }

    @OnClick({R.id.view_bind_school, R.id.view_unbind_school, R.id.view_update, R.id.view_modify_password, R.id.view_system})
    public void onViewClicked(View view) {
        cancelDownTimer();
        switch (view.getId()) {
            case R.id.view_bind_school://切换学校
                BindSchoolFragment changeSchoolFragment = BindSchoolFragment.newInstance(true);
                changeSchoolFragment.show(getSupportFragmentManager(), "changeSchoolFragment");
                break;
            case R.id.view_unbind_school://解除绑定
                UnbindSchoolFragment unbindSchoolFragment = UnbindSchoolFragment.newInstance();
                unbindSchoolFragment.show(getSupportFragmentManager(), "unbindSchoolFragment");
                break;
            case R.id.view_update://版本更新
                checkAppVersion();
                break;
            case R.id.view_modify_password://修改密码
                ModifyPasswordFragment modifyPasswordFragment = ModifyPasswordFragment.newInstance();
                modifyPasswordFragment.show(getSupportFragmentManager(), "modifyPasswordFragment");
                break;
            case R.id.view_system:
                Intent intent = new Intent(Settings.ACTION_SETTINGS);
                startActivityForResult(intent, 525);
                break;
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBusMessage(EventMessage bean) {
        String message = bean.getMessage();
        switch (message) {
            case AppConstant.CHANGE_SCHOOL://切换学校
                LogUtils.e("切换学校成功");
                tvSchoolName.setText(loadSchoolName());
                break;
            case AppConstant.SETTING://解绑成功
                EventBus.getDefault().post(new EventMessage(AppConstant.UNBIND_SCHOOL));
                onClickBack();
                break;
            case AppConstant.FRAGMENTDIALOG_DISMISS://FragmentDiag关闭
                initDownTimer();
                break;
        }
    }

    @Override
    public void onSuccess(String data, String aesKey, String message, int code, String tag) {
        super.onSuccess(data, aesKey, message, code, tag);
        AppBean bean = new Gson().fromJson(data, AppBean.class);
        String version = bean.getVersion();
        int currentVersion = CommonUtil.getVersionCode(mContext);
        version = version.charAt(0) + version.substring(2, 3) + version.charAt(4);
        int newVersion = Integer.parseInt(version);
        LogUtils.e("newVersion=" + newVersion + "==currentVersion=" + currentVersion);
        if (newVersion > currentVersion) {
            UpdateAppFragment updateAppFragment = UpdateAppFragment.newInstance(bean);
            updateAppFragment.show(getSupportFragmentManager(), "update");
        } else {
            ToastUtil.toast("当前已是最新版本");
        }
    }


    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 10086) {
            //安装apk
            UpdateUtil.getInstant().installProcess(this, getPackageManager());
        } else if (requestCode == 525) {
            resetDownTimer();
        }
    }
}